package com.greenline.search.report;

import com.greenline.search.report.entity.LogReportResult;
import com.greenline.search.report.entity.SearchWord;
import com.greenline.search.report.model.Configuration;
import com.greenline.search.report.model.GenerateReportResult;
import com.greenline.search.report.util.*;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-2-12
 * Time: 下午2:39
 * To change this template use File | Settings | File Templates.
 */
public class SearchLogReportEmail {
    private static Logger logger = LoggerFactory.getLogger(SearchLogReportEmail.class);
    //上周日志文件名称列表（不要被名字所迷惑）
    private List<String> currentWeekLogNameList;
    //上周搜索词列表
    private List<SearchWord> currentWeekSearchWordList;

    //上上周日志文件名称数组（不要被名字所迷惑）
    private List<String> lastWeekLogNameList;
    //上上周搜索词列表
    private List<SearchWord> lastWeekSearchWordList;

    public SearchLogReportEmail(){
        Date today = new Date();
        currentWeekLogNameList = Arrays.asList(DateUtils.getLastWeekLogNameArray(today,Configuration.WEEK_DAY_COUNT));
        lastWeekLogNameList = Arrays.asList(DateUtils.getLastWeekLogNameArray(DateUtils.getLastWeekToday(today),Configuration.WEEK_DAY_COUNT));
        currentWeekSearchWordList = new LinkedList<SearchWord>();
        lastWeekSearchWordList = new LinkedList<SearchWord>();
        SearchWordListUtil.initSearchWordList(currentWeekLogNameList,currentWeekSearchWordList);
        SearchWordListUtil.initSearchWordList(lastWeekLogNameList,lastWeekSearchWordList);
    }

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();

        String[] currentLognameArray = DateUtils.getLastWeekLogNameArray(new Date(),Configuration.WEEK_DAY_COUNT);
        String timeLine = "("+currentLognameArray[0]+"-"+currentLognameArray[currentLognameArray.length-1]+")";
        String fileName = "log_report_result_"+timeLine;
        System.out.println(Configuration.OBJECT_PATH);
        LogReportResult reportResult = (LogReportResult) ObjectSerializationUtil.readReportResult(fileName);
        if(reportResult == null){
            logger.info("Get log report result...");
            SearchLogReportEmail searchLogReport = new SearchLogReportEmail();
            GenerateReportResult generateReportResult =
                    new GenerateReportResult(searchLogReport.getCurrentWeekSearchWordList(),
                            searchLogReport.getLastWeekSearchWordList());

            reportResult = generateReportResult.getCurrentWeekReportResult();
            ObjectSerializationUtil.writeReportResult(fileName,reportResult);
        }
        String content = FormatLogResultUtil.getAllFormatedResult(reportResult);
        EmailUtils.sendEmail("搜索数据周报"+timeLine, content);
        logger.info("Total cost time {} seconds.", (System.currentTimeMillis()-startTime)/1000);
    }


    public List<SearchWord> getCurrentWeekSearchWordList() {
        return currentWeekSearchWordList;
    }

    public List<SearchWord> getLastWeekSearchWordList() {
        return lastWeekSearchWordList;
    }
}